<?php

class FoodPlaces extends TPage
{
	private $_maxPage = 1;
	private $_currentPage = 1;
	private $_queryParams = array("p","st","sb","b","balias","mf","q","c","calias","subc","subcalias","best_seller","new_arrival","promotion");
	
	public function setMaxPage($value)
	{
		$this->_maxPage = TPropertyValue::ensureInteger($value);
	}

	public function getMaxPage()
	{
		return $this->_maxPage;
	}

	public function setCurrentPage($value)
	{
		$this->_currentPage = TPropertyValue::ensureInteger($value);
	}

	public function getCurrentPage()
	{
		return $this->_currentPage;
	}
	
	public function getQueryParameters()
	{
		return $this->_queryParams;
	}
	
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->CurrentPage = ($this->Request->contains('p')) ? TPropertyValue::ensureInteger($this->Request['p']) : 1;
		
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " - Food Places";
			$this->Master->cssMenuFoodPlace->Value = "current_menu_item";
			
			// Populate Cuisine List
			$this->cboCuisineSelector->DataSource = CuisineRecord::finder()->getAllItems();
			$this->cboCuisineSelector->DataBind();
			
			// Populate MRT List
			$this->cboMRTSelector->DataSource = MRTRecord::finder()->getAllItems();
			$this->cboMRTSelector->DataBind();
			
			// Populate Close Time List
			$this->cboCloseTimeSelector->DataSource = CloseTimeRecord::finder()->getAllItems();
			$this->cboCloseTimeSelector->DataBind();
			
			$sql = "";
			if ($this->Request->contains('mrt') && $this->Request['mrt'] != '') {
				$sql .= " and f.nearest_mrt = " . $this->Request['mrt'];
				$this->cboMRTSelector->SelectedValue = $this->Request['mrt'];
			}
			
			if ($this->Request->contains('time') && $this->Request['time'] != '') {
				$sql .= " and f.open_until >= " . $this->Request['time'];
				$this->cboCloseTimeSelector->SelectedValue = $this->Request['time'];
			}
			
			if ($this->Request->contains('cid') && $this->Request['cid'] != '') {
				$sql .= " and fc.cuisine_id = " . $this->Request['cid'];
				$this->cboCuisineSelector->SelectedValue = $this->Request['cid'];
			}
			
			$this->renderBoxRecommendedFoodPlaces($sql);
		}
	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			//$this->lblSearchResult->Visible=false;
			
			$sql = "";
			if ($this->cboMRTSelector->SelectedValue > 0) {
				$sql .= " and f.nearest_mrt = " . $this->cboMRTSelector->SelectedValue;
			}
			
			if ($this->cboCloseTimeSelector->SelectedValue > 0) {
				$sql .= " and f.open_until >= " . $this->cboCloseTimeSelector->SelectedValue;
			}
			
			if ($this->cboCuisineSelector->SelectedValue > 0) {
				$sql .= " and fc.cuisine_id = " . $this->cboCuisineSelector->SelectedValue;
			}
			
			$this->renderBoxRecommendedFoodPlaces($sql);
		}
	}
	
	protected function renderBoxRecommendedFoodPlaces($sql)
	{
		
		$limit = null;
		$offset = null;
		$sqlmap = $this->Application->Modules['sqlmap']->Client;
		
		$this->ListFoodPlace->VirtualItemCount = count($sqlmap->queryForList("SearchFoodPlace", array("ADDITIONAL_CONDITION"=>$sql)));
		$totalItems = $this->ListFoodPlace->VirtualItemCount;
		$this->MaxPage = ceil($this->ListFoodPlace->VirtualItemCount/$this->ListFoodPlace->PageSize);
        if ($this->CurrentPage > $this->MaxPage) $this->CurrentPage = $this->MaxPage;
        $limit = $this->ListFoodPlace->PageSize;
        $offset = ($this->CurrentPage-1) * $limit;

        if ($offset + $limit > $this->ListFoodPlace->VirtualItemCount)
            $limit = $this->ListFoodPlace->VirtualItemCount - $offset;
			
		$items = $sqlmap->queryForList("SearchFoodPlace",array("ADDITIONAL_CONDITION"=>$sql),null,$offset,$limit);
		$this->ListFoodPlace->DataSource = $items;
		$this->ListFoodPlace->DataBind();
		
		//$this->lblSearchResult->Text=$totalItems." Food Places <font color='black' style='text-transform:capitalize;'>| Page</font> " . $this->CurrentPage . " <font color='black' style='text-transform:capitalize;'>of</font> ".$this->MaxPage;
		
		if (count($items) <= 0)
		{
			//$this->lblSearchResult->Text="Your search did not match any food places.";
			//$this->lblSearchResult->Visible=true;
		}	
	}
}

?>